package ink.xiaobaibai.serviceOxy.monthly.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ink.xiaobaibai.entity.MonthlyfeeOrder;
import ink.xiaobaibai.enumOxy.MonthlyfeeOrderStatus;
import ink.xiaobaibai.service.IMonthlyUserDeviceService;
import ink.xiaobaibai.service.IMonthlyfeeOrderService;
import ink.xiaobaibai.serviceOxy.monthly.IDelayMonControlService;
import ink.xiaobaibai.serviceOxy.monthly.IOrderMonService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;

/**
 * @description: 月费服务层
 * @author: 小白白
 * @create: 2021-06-01
 **/

@Service
public class OrderMonServiceImpl implements IOrderMonService {

    @Autowired
    private IMonthlyfeeOrderService orderService;

    @Autowired
    private IMonthlyUserDeviceService userDeviceService;

    @Autowired
    private IDelayMonControlService delayMonService;

    @Override
    public void activateDevice(String orderId) {
        /**
         * 1.订单使用状态,确定过期时间 2.用户机器映射表 可用状态
         */
        QueryWrapper<MonthlyfeeOrder> q1 = new QueryWrapper<>();
        q1.select("order_id", "status", "user_id", "device_id");
        q1.eq("order_id", orderId);
        MonthlyfeeOrder order = this.orderService.getOne(q1);
        if (order.getStatus().equals(MonthlyfeeOrderStatus.ACTIVATED.getStatus())) {
            this.activate(order);
        }
    }

    @Override
    public void activateDevice(Integer deviceId, Integer userId) {
        QueryWrapper<MonthlyfeeOrder> q1 = new QueryWrapper<>();
        q1.select("order_id", "status", "user_id", "device_id");
        q1.eq("device_id", deviceId);
        q1.eq("user_id", userId);
        q1.eq("status", MonthlyfeeOrderStatus.ACTIVATED.getStatus());
        MonthlyfeeOrder order = this.orderService.getOne(q1);
        if (order != null && order.getStatus().equals(MonthlyfeeOrderStatus.ACTIVATED.getStatus())) {
            this.activate(order);
        }
    }

    //激活
    private void activate(MonthlyfeeOrder order) {
        //未激活,那就把它激活
        Integer trialDay = order.getTrialDay();
        Integer extendedDay = order.getExtendedDay();
        //今天晚上的23:59分+30+延期
        order.setExpireTime(DateUtils.addDays(this.getEndTimeOfDay(), (trialDay + extendedDay + 30)));
        order.setStatus(MonthlyfeeOrderStatus.INUSE.getStatus());
        order.setOrderId(order.getOrderId());
        this.orderService.updateById(order);
        this.delayMonService.removeActivation(order.getOrderId());
    }

    /**
     * 获取今天的23:59:00秒
     */
    private Date getEndTimeOfDay() {
        String startTimeStr = DateFormatUtils.format(new Date(), "yyyy-MM-dd 23:59:00");
        try {
            return DateUtils.parseDate(startTimeStr, "yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return DateUtils.addDays(new Date(), 1);
    }

}
